/**
 * common.js
 * General javascripts that always are loaded. 
 * @copyright	Copyright(c) 2002-2007 Bjorn Winberg
 * @author		Bjorn Winberg <cms@anomalye.net>
 * @license		http://www.gnu.org/licenses/gpl.html GNU General Public License version 2 
 * @package		AnomalyWiki
 */

/**
 *
 */ 
function ajax_xmlHttpSend(url, targetElement){
	var xmlHttp;
	if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		return false;
	}
	xmlHttp.onreadystatechange = function(){ajax_handleStateChanged(xmlHttp, targetElement);};
	xmlHttp.open("GET", url, true);
	xmlHttp.send("");
}

/**
 *
 */
function ajax_handleStateChanged(xmlHttp, targetElement) {
	if (xmlHttp.readyState == 4) { 
		if (xmlHttp.status == 200) { 
			document.getElementById(targetElement).innerHTML = xmlHttp.responseText;
		} else {
			document.getElementById(targetElement).innerHTML = '';
		}
	}
}

/**
 *
 */
function addLoadEvent(func){ 
	var oldonload = window.onload; 
	if(typeof window.onload != 'function'){ 
		window.onload = func; 
	}
	else{ 
		window.onload = function(){ 
			if(oldonload){ 
				oldonload(); 
			}
			func(); 
		} 
	} 
} 

/*function ajax_sendForm(obj){
  var getstr = "";
  for (i=0; i<obj.getElementsByTagName("input").length; i++) {
        if (obj.getElementsByTagName("input")[i].type == "text") {
           getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + obj.getElementsByTagName("input")[i].value;
        }
        if (obj.getElementsByTagName("input")[i].type == "checkbox") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + obj.getElementsByTagName("input")[i].value;
           } else {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=";
           }
        }
        if (obj.getElementsByTagName("input")[i].type == "radio") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + obj.getElementsByTagName("input")[i].value;
           }
     }  
     if (obj.getElementsByTagName("input")[i].tagName == "SELECT") {
        var sel = obj.getElementsByTagName("input")[i];
        getstr += "&" + sel.name + "=" + sel.options[sel.selectedIndex].value;
     }
  }
  getstr += "&o=ajax";
  return ajax_xmlHttpSend(obj.action + getstr);
}

function ajax_onSubmit(obj, targetId){
	document.getElementById(targetId).innerHTML = ajax_sendForm(obj);
	if(document.getElementById('ajaxHiddenFeedback') != null){
		document.getElementById('ajaxFeedback').innerHTML = document.getElementById('ajaxHiddenFeedback').innerHTML;
		document.getElementById(targetId).removeChild(document.getElementById('ajaxHiddenFeedback'));
	}
	return false; 
}*/
 
/**
 * Add a leading zero on a number smaller than 10.
 */ 
function leadingZero(number){
	if(number < 10){
		number = "0" + number;
	}
	return number;
}

/**
 * Return a string about the client GMT offset.
 */
function getGMTStr(){
	var offset = new Date().getTimezoneOffset();
	offset = (offset < 0? '+': '-') +
		leadingZero(Math.abs(offset/60)) + ':' +
		leadingZero(Math.abs(offset%60));
	return offset;
}

/**
 * Return a timestamp string according to format.
 */
function getTime(timestamp, format){
	var ret = new Date(timestamp * 1000);
	var year = ret.getFullYear();
	var month = ret.getMonth() + 1;
	var day = ret.getDate();
	var hour = ret.getHours();
	var minute = ret.getMinutes();
	var second = ret.getSeconds();
	var retStr = new String(format);
	retStr = retStr.replace("H", leadingZero(hour)).replace("i", leadingZero(minute)).replace("s", leadingZero(second)).replace("Y", year).replace("m", leadingZero(month)).replace("d", leadingZero(day));
	return retStr;
}
